package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.base.utils.EnumUtil;
import com.weixing.mall.core.support.auth.AuthUtil;
import com.weixing.mall.provider.mapper.OrderMapper;
import com.weixing.mall.provider.model.domain.Order;
import com.weixing.mall.provider.model.enums.OrderFromEnum;
import com.weixing.mall.provider.model.enums.OrderFrontStateEnum;
import com.weixing.mall.provider.model.vo.FrontOrderGoodsVo;
import com.weixing.mall.provider.model.vo.member.MemberOrderVo;
import com.weixing.mall.provider.service.IMemberOrderService;
import com.weixing.mall.provider.service.IOrderGoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 订单模块-订单表 服务实现类
 * </p>
 *
 * @author siashan
 * @since 2.0
 */
@Service
public class MemberOrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IMemberOrderService {

    @Autowired
    private IOrderGoodsService orderGoodsService;
    /**
     * 会员未认领订单列表
     *
     * @apiNote 查询会员所有未认领订单包括正价和秒杀
     * @param page    分页
     * @return 未认领订单列表
     */
    @Override
    public IPage<MemberOrderVo> noClaimOrders(Page page) {
        Long mid = AuthUtil.getMemberExt().getMid();
        IPage<Order> orderIPage = null;
        // 查询订单列表
        QueryWrapper<Order> wrapper = new QueryWrapper();
        wrapper.eq("buyer_id", mid)
                .eq("order_from", OrderFromEnum.MEMBER.getValue())
                .eq("is_del", WhetherEnum.NO.getValue());
        wrapper.orderByDesc("id");
        orderIPage = page(page, wrapper);
        List<Order> records = orderIPage.getRecords();
        List<MemberOrderVo> frontOrderVoList = Lists.newArrayList();
        MemberOrderVo frontOrderVo = null;
        if (null != records && !records.isEmpty()) {
            for (Order order : records) {
                frontOrderVo = new MemberOrderVo();
                BeanUtils.copyProperties(order,frontOrderVo);
                frontOrderVo.setOrderId(order.getId());
                frontOrderVo.setFrontOrderStatus(EnumUtil.getIEnum(OrderFrontStateEnum.class, order.getFrontOrderStatus()));
                List<FrontOrderGoodsVo> orderGoodss = orderGoodsService.selectFrontOrderGoodsVoByOrderId(order.getId());
                frontOrderVo.setOrderGoods(orderGoodss);
                frontOrderVoList.add(frontOrderVo);
            }
        }
        Page<MemberOrderVo> orderVoPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        orderVoPage.setRecords(frontOrderVoList);
        return orderVoPage;
    }
}
